package persistencia;
import entidade.Topico;
import java.util.ArrayList;
import java.util.List;


	public class TopicoDao extends Dao {

	    public void cadastrar(Topico t)throws Exception{

	        conectar();
	        stmt = con.prepareStatement("insert  into topico (titulo, codDisciplina) values(?, ?)");
	        stmt.setString(1, t.getTitulo());
	        stmt.setInt(2, t.getCodDisciplina());
	        stmt.execute();
	        desconectar();
	    }

	    public List<Topico> consultaGeral()throws Exception{
	        conectar();
	        List<Topico> lista = new ArrayList<Topico>();
	        stmt = con.prepareStatement("select * from topico");
	        rs = stmt.executeQuery();

	        while(rs.next()){
	            Topico t = new Topico();
	            t.setIdTopico(rs.getInt("idTopico"));
	            t.setTitulo(rs.getString("titulo"));
	            t.setCodDisciplina(rs.getInt("codDisciplina"));
	            lista.add(t);
	        }
	        desconectar();
	        return lista;
	    }


	    public void deletar(int idTopico)throws Exception{
	        conectar();
	        stmt = con.prepareStatement("delete from topico where idTopico = ?");
	        stmt.setInt(1, idTopico);
	        stmt.execute();
	        desconectar();
	    }

	    public void editar(Topico t)throws Exception{
	        conectar();
	        stmt = con.prepareStatement("update topico set titulo = ? set codDisciplina = ? where idTopico = ?");
	        stmt.setString(1, t.getTitulo());
	        stmt.setInt(2, t.getCodDisciplina());
	        stmt.setInt(3, t.getIdTopico());
	        stmt.execute();
	        desconectar();
	    }

	    public Topico consultarDados(int idTopico)throws Exception{
	        Topico t = null;
	        conectar();
	        stmt = con.prepareStatement("select * from topico where idTopico =?");
	        stmt.setInt(1, idTopico);
	        rs = stmt.executeQuery();

	        if(rs.next()){
	            t = new Topico();
	            t.setIdTopico(rs.getInt("idTopico"));
	            t.setTitulo(rs.getString("titulo"));
	            t.setCodDisciplina(rs.getInt("codDisciplina"));
	            
	        }
	      return t;
	    }

}




